import { ipcRenderer, contextBridge } from 'electron'

// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', {
  on(...args: Parameters<typeof ipcRenderer.on>) {
    const [channel, listener] = args
    return ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
  },
  off(...args: Parameters<typeof ipcRenderer.off>) {
    const [channel, ...omit] = args
    return ipcRenderer.off(channel, ...omit)
  },
  send(...args: Parameters<typeof ipcRenderer.send>) {
    const [channel, ...omit] = args
    return ipcRenderer.send(channel, ...omit)
  },
  invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
    const [channel, ...omit] = args
    return ipcRenderer.invoke(channel, ...omit)
  },

  // You can expose other APTs you need here.
  // ...
})

// 暴露文件处理相关的 API
contextBridge.exposeInMainWorld('electronAPI', {
  // 监听文件内容接收
  onFileContentReceived: (callback: (data: {
    filePath: string
    content: string
    timestamp: string
  }) => void) => {
    ipcRenderer.on('file-content-received', (_event, data) => callback(data))
  },

  // 通知主进程文件上传完成
  notifyUploadCompleted: (data: any) => {
    ipcRenderer.send('file-upload-completed', data)
  },

  // 通知主进程文件上传失败
  notifyUploadFailed: (error: any) => {
    ipcRenderer.send('file-upload-failed', error)
  }
})
